package com.example.fooservice;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * TODO
 *
 * @author zhangjun3
 * @date 2022/3/6 17:16
 */
//@Component
public class RedisTestFilter implements GlobalFilter {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ReactiveRedisTemplate reactiveRedisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return reactiveRedisTemplate.opsForValue()
                .set("time", new Date().toString())
                .then(reactiveRedisTemplate.opsForValue().get("time"))
                .flatMap((v) -> {
                    // some logic here
                    return chain.filter(exchange);
                });
//        原有阻塞式写法
//        redisTemplate.opsForValue().set("time", new Date().toString());
//        redisTemplate.opsForValue().get("time");
//        return chain.filter(exchange);
    }
}
